dnl Process this file with autoconf to produce a configure script.
# Copyright (C) 2000-2012, 2016, 2019 Free Software Foundation, Inc.
# Copyright (C) 2020 Maxime Devos
#
# Author (GnuTLS): Nikos Mavrogiannopoulos, Simon Josefsson
# Author (rehash): Maxime Devos
#
# This file is part of rehash
#
# rehash is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or (at
# your option) any later version.
#
# rehash is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with rehash.  If not, see <http://www.gnu.org/licenses/>.

# This file is partially based upon the Autoconf script of GnuTLS.

AC_PREREQ([2.61])
AC_INIT([rehash], [0], [])
AC_CONFIG_AUX_DIR([build-aux])
AC_CONFIG_MACRO_DIRS([m4 m4/gl])

AM_INIT_AUTOMAKE([foreign subdir-objects])

AC_CONFIG_HEADERS([rehash_config.h])

AC_MSG_RESULT([***
*** Checking for compilation programs...
])
dnl Checks for programs.
PKG_PROG_PKG_CONFIG
AC_PROG_CC


dnl GNU Guile support
AC_MSG_CHECKING([whether building rehash bindings])
AC_ARG_ENABLE(guile,
        AS_HELP_STRING([--enable-guile], [build GNU Guile bindings]),
                [opt_guile_bindings=$enableval], [opt_guile_bindings=yes])
AC_MSG_RESULT($opt_guile_bindings)

AC_ARG_WITH([guile-site-dir], AS_HELP_STRING([--with-guile-site-dir=DIR],
    [guile site directory for rehash, default is guile system settings]),
    [guilesitedir="${withval}"], [guilesitedir='$(GUILE_SITE)'])
AC_ARG_WITH([guile-site-ccache-dir], AS_HELP_STRING([--with-guile-site-ccache-dir=DIR],
    [guile ccache directory for rehash, default is guile system settings]),
    [guilesiteccachedir="${withval}"], [guilesiteccachedir='$(GUILE_SITE_CCACHE)'])
AC_SUBST([guilesitedir])
AC_SUBST([guilesiteccachedir])

if test "$opt_guile_bindings" = "yes"; then
   AC_MSG_RESULT([***
*** Detecting GNU Guile...
])

   dnl Check for 'guild', which can be used to compile Scheme code
   dnl on Guile 2.x and 3.x.
   AC_PATH_PROG([GUILD], [guild])
   AC_SUBST([GUILD])

   dnl TODO check if 2.x works
   GUILE_PKG([3.0 2.2 2.0])
   GUILE_PROGS
   GUILE_SITE_DIR
   GUILE_FLAGS
fi

AM_CONDITIONAL([HAVE_GUILE], [test "$opt_guile_bindings" = "yes"])
AM_CONDITIONAL([HAVE_GUILD], [test "x$GUILD" != "x"])

# Dynamic library support
# TODO: doesn't pick up system libtool automatically
LT_INIT

# Check for GNUnet libraries
PKG_CHECK_MODULES([GNUNETDHT],  [gnunetdht])
PKG_CHECK_MODULES([GNUNETUTIL], [gnunetutil])
PKG_CHECK_MODULES([GNUNETDATASTORE], [gnunetdatastore])
# TODO perhaps include gnunetcrypto as well
# And some GNUnet dependencies
PKG_CHECK_MODULES([LIBEXTRACTOR], [libextractor])

# TODO perhaps implement some language bindings:
#  [ ] haskell
#  [ ] guile (+ other schemes) (<-- priority)
#  [ ] common lisp
#  [ ] emacs
#  [ ] rust

GNUNETUTIL_LIBDIR="`$PKG_CONFIG --variable libdir gnunetutil`"
AC_SUBST([GNUNETUTIL_LIBDIR])
AC_CONFIG_FILES([scheme-binding/rehash/config.scm])

AC_OUTPUT([Makefile])

AC_MSG_NOTICE([Language bindings:

  Guile wrappers: $opt_guile_bindings
  C library:      yes
])
